﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media.Animation;
using EzDesk.Generic;

namespace EzDesk.Generic.WPF
{
    public static class UIElementExtension
    {
        public static T Show<T>(this T source) where T : UIElement
        {
            source.Visibility = Visibility.Visible;
            return source;
        }
        public static T Collapsed<T>(this T source) where T : UIElement
        {
            source.Visibility = Visibility.Collapsed;
            return source;
        }
        public static T Hide<T>(this T source) where T : UIElement
        {
            source.Visibility = Visibility.Hidden;
            return source;
        }
        public static T GetUIElementFromXamlString<T>(this string xamlString) where T : UIElement
        {
            return System.Windows.Markup.XamlReader.Parse(xamlString) as T;
        }

        public static void HideSlowly(this System.Windows.UIElement source)
        {
            source.SetOpacityAnimationTo(TimeSpan.FromSeconds(1), 0, 0.5);
        }
        public static void ShowSlowly(this System.Windows.UIElement source)
        {
            source.SetOpacityAnimationTo(TimeSpan.FromSeconds(0.6), 0.6, 1, 0.5);
        }
        private static void SetOpacityAnimationTo(this System.Windows.UIElement source, TimeSpan timespan, double fromValue, double toValue, double speedRatio)
        {
            Storyboard sb = new Storyboard();
            DoubleAnimation da = new DoubleAnimation();
            da.Duration = new Duration(timespan);
            da.From = fromValue;
            da.To = toValue;
            da.SpeedRatio = speedRatio;
            da.SetTarget(source, UIElement.OpacityProperty);
            sb.Children.Add(da);
            sb.Begin();
        }
        public static void SetOpacityAnimationTo(this System.Windows.UIElement source, TimeSpan timespan, double toValue, double speedRatio)
        {
            Storyboard sb = new Storyboard();
            DoubleAnimation da = new DoubleAnimation();
            da.Duration = new Duration(timespan);
            da.To = toValue;
            da.SpeedRatio = speedRatio;
            da.SetTarget(source, UIElement.OpacityProperty);
            sb.Children.Add(da);
            sb.Begin();
        }
        public static void MinimizeCurrentWindow(this System.Windows.UIElement source)
        {
            Window currentWindow = source.FindParentWindow();

            if (currentWindow != null)
            {
                currentWindow.WindowState = WindowState.Minimized;
                //currentWindow.Hide();
            }
        }

        public static void HideCurrentWindow(this System.Windows.UIElement source)
        {
            Window currentWindow = source.FindParentWindow();

            if (currentWindow != null)
            {
                currentWindow.Hide();
            }
        }

        public static void CloseCurrentWindow(this System.Windows.UIElement source)
        {
            Window currentWindow = source.FindParentWindow();

            if (currentWindow != null)
            {
                currentWindow.Close();
            }
        }

        public static void MoveCurrentWindow(this System.Windows.UIElement source)
        {
            Window currentWindow = source.FindParentWindow();

            if (currentWindow != null)
            {
                currentWindow.DragMove();
            }
        }

        public static Window FindParentWindow(this System.Windows.UIElement source)
        {
            DependencyObject parent = source;
            Window currentWindow = source as Window;

            while ((parent = LogicalTreeHelper.GetParent(parent)) != null)
            {
                currentWindow = parent as Window;
                if (currentWindow != null) break;
            }
            return currentWindow;
        }
    }
}
